<?php
/**
 * Abstract class for SearchableComponent.
 * @author Shrey
 *
 */
abstract class AbstractSearchableComponent extends Object
{
	/**
	 * 
	 * @var unknown_type
	 */
	var $autoFilter = true;
	
	/**
	 * 
	 * @var unknown_type
	 */
	var $modelAlias = null;
	/**
	 * 
	 * @param unknown_type $controller
	 * @param unknown_type $settings
	 */
	function initialize($controller, $settings) 
	{
		$this->_controller = $controller;
		$this->_controller->helpers[] = 'Searchable.SearchForm';
		$this->_controller->params['searchable'] = $settings;
		$this->modelAlias = $this->_controller->modelNames[0];
	}
	
	function startup($controller)
	{
		$this->_controller = $controller;
		$this->_controller->helpers[] = 'Searchable.SearchForm';
		//pr($_POST);
		if($this->_controller->params['controller']=='saved_searches')
			return;
		if($this->isFilterApplied())
		{
			if($this->autoFilter)
				$this->applyFilters();
		}
		else
		{
			// check if search performed for first time
			if(!empty($this->_controller->data))
			{
				if(!isset($this->_controller->data['Search']['key']))
				{
					return;
				}
				//save search if title has been entered
				$searchResultsAction = array(
					'plugin'=>$this->_controller->params['plugin'],
					'controller'=>$this->_controller->params['controller'],
					'action'=>$this->_controller->action,
					'named' => $this->_controller->params['named'],
				);
				$searchResultsAction = am($searchResultsAction,$this->_controller->params['pass']);
				if(isset($this->_controller->params['form']['button']) && $this->_controller->params['form']['button']== 'Save' && isset($this->_controller->data['SavedSearch']['title']) && strlen($this->_controller->data['SavedSearch']['title']) > 1)
				{
					$this->saveSearch($searchResultsAction);
				}
				$searchKey = $this->_controller->data['Search']['key'];
				$this->cacheSearch($searchKey);
				$searchResultsAction['search'] = $searchKey;
				$this->_controller->redirect($searchResultsAction);
			}
		}
	}
	
	/**
	 * Caches search data
	 * @param unknown_type $searchId
	 */
	function cacheSearch($searchId,$data =null)
	{
		$searchKey = 'search.'.$searchId;
		if($data===null)
		{
			$data = $this->_controller->data;
		}
		Cache::set(array('duration'=>"+2 days","path"=>CACHE));
		Cache::write($searchKey,$data);
	}
	
	/**
	 * Return searh data
	 * @param unknown_type $searchId
	 */
	function getSearchParams($searchId)
	{
		if(strpos($searchId,'saved_')!==false)
		{
			$searchId = array_pop(explode('_',$searchId));
			$params = $this->getSavedSearchParam($searchId);
			unset($this->_controller->params['named']['search']);
			if($params)
			{
				$searchId = $searchId.'_'.time();
				$this->cacheSearch($searchId,$params);
				$this->_controller->params['named']['search'] = $searchId;
				return $params;
			}else
			{
				return array();
			}
		}
		$searchKey = 'search.'.$searchId;
		Cache::set(array('duration'=>"+2 days","path"=>CACHE));
		return Cache::read($searchKey);
	}
	/**
	 * Return whether filter has been applied or not.
	 */
	function isFilterApplied()
	{
		if(isset($this->_controller->params['named']['search']))
		{
			return true;
		}
		return false;
	}
	/**
	 * Perform nesscary call's for appltying filter on present request
	 * @param $filterName
	 */
	function applyFilters($filterName = null)
	{
			
		$this->_controller->data = $this->getSearchParams($this->_controller->params['named']['search']);
		if($filterName == null)
		{
			$filterName = $this->_controller->data['Search']['searchType'];
		}
		$this->_controller->paginate['conditions'] = $this->getConditions($filterName);
	}
	
	/**
	 *Returns search conditions for given filter.
	 * @param $filterName
	 */
	function getConditions($filterName)
	{
		return $this->_controller->{$this->modelAlias}->createCondition($filterName,$this->_controller->data['Search']);
	}
	
	/**
	 * Saves a search in runtime.
	 * @todo user's access
	 */
	function saveSearch($searchResultsAction)
	{
		if(!$this->_controller->Session->read('Auth.User'))
			return;
		$title = $this->_controller->data['SavedSearch']['title'];
		unset($this->_controller->data['SavedSearch']);
		
		$data['SavedSearch'] = array('title'=>$title,'searchParams'=>$this->_controller->data,
									'owner_entity'=>'User','owner_entity_id'=>$this->_controller->Session->read('Auth.User.id'),
									'search_url'=>$searchResultsAction,
									'type' => $this->modelAlias
								);
		cri('Searchable.SavedSearch')->save($data);
	}
	
	/**
	 * Find searchParams for give search id
	 * @param $searchId
	 */
	function getSavedSearchParam($searchId)
	{
		$savedSearch = cri('Searchable.SavedSearch')->findById($searchId);
		$params = $savedSearch['SavedSearch']['searchParams'];
		return $params;
	}
}
?>